from urllib import request
from CoTec.utility.string.string_go import StringHelper as sh
from CoTec.utility.string.string_go import genUUID

# import asyncio

class Download():

    def __init__(self, config):
        self.config = config
        self.__stroe_path = config.FILE_PATH_PRIFIX() + config.PIC_PART_PATH()
        self.__part_path_prifix = config.PIC_PART_PATH()

    def img(self, file_path:str) -> str:
        """
        异步下载，先行返回结果，错路暂不考虑，这个坑以后甜
        :param file_path:
        :return:
        """
        try:
            suffix = sh.extra_suffix(file_path)
            name = genUUID() + '.' + suffix
            path = sh.make_path(self.__stroe_path, name)
            return_path = sh.make_path(self.__part_path_prifix, '/' + name)

            request.urlretrieve(file_path, path)

            return return_path
        except Exception as e:
            print(str(e))

    # async def get_json(client, url):
    #     async with client.get(url) as response:
    #         assert response.status == 200
    #         return await response.read()

# url = 'http://img1.gtimg.com/ent/pics/hv1/164/213/2187/142264154.jpg'
#
# d = download('/Users/momachuanmei/Documents/XProject/PyKindom/Download/img')
# loop = asyncio.get_event_loop()
# p = asyncio.ensure_future(d.img(url))
# loop.run_forever()
#
# # p = d.img(url)
# print(p)


